# Definition for a binary tree node.


class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Codec:

    def serialize(self, root):
        que = []
        que.append(root)
        res = ""
        while(len(que) != 0):
            node = que.pop(0)
            if(node != None):
                que.append(node.left)
                que.append(node.right)
            value = 'null' if(node == None) else str(node.val)
            res = res+"," + value
        return res if(len(res) == 0) else res[1:len(res)]

    def deserialize(self, data):
        que = []
        array = data.split(",")
        if(len(array) == 0 or array[0] == 'null'):
            return None
        root = TreeNode(int(array[0]))
        que.append(root)
        for i in range(1, len(array)+2, 2):
            if(len(que) == 0):
                break
            t = que.pop(0)
            t.left = None if(i >= len(array) or array[i] == 'null') else TreeNode(
                int(array[i]))
            t.right = None if(
                i+1 >= len(array) or array[i+1] == 'null') else TreeNode(int(array[i+1]))
            if(t.left != None):
                que.append(t.left)
            if(t.right != None):
                que.append(t.right)
        return root


if __name__ == "__main__":
    # r = Codec().deserialize("1,2,3,null,null,4,5")
    r = Codec().deserialize("1,2,3,null,null,4,5,null,null,null,null")
    print(Codec().serialize(r))
